package cn.didi.feidi.androidorder.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.adapter.MainOrderViewPagerAdapter;
import cn.didi.feidi.androidorder.assembly.FeidiActionBar;
import cn.didi.feidi.androidorder.databinding.FragmentMainOrderBinding;

/**
 * Created by jinxiong on 2017/8/19.
 */

public class OrderFragment extends FeiDiMainBaseFragment {

    FragmentMainOrderBinding binding;
    MainOrderViewPagerAdapter adapter;
    private ArrayList<MainOrderItemFragment> fragments;
    private int currentPage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragments = new ArrayList<>();

        for (int i = 0; i < 3; i++) {

            MainOrderItemFragment fragment = new MainOrderItemFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("status", i);
            fragment.setArguments(bundle);

            fragments.add(fragment);
        }

        //已完成的状态为4
        MainOrderItemFragment fragment = new MainOrderItemFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("status", 4);
        fragment.setArguments(bundle);

        fragments.add(fragment);


        adapter = new MainOrderViewPagerAdapter(getChildFragmentManager(), fragments);

    }

    @Override
    protected View initFragment(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_main_order, container, false);
        return binding.getRoot();

    }

    @Override
    protected void initViews(View rootView) {

        binding.mainOrderViewPager.setAdapter(adapter);
        binding.mainOrderTabLayout.setupWithViewPager(binding.mainOrderViewPager);


    }

    @Override
    protected void initData() {


    }

    @Override
    protected void addListener() {
        binding.mainOrderViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                fragments.get(currentPage).cancelRefresh();
                fragments.get(position).getDataFromNet(false, false, true);
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void actionBarSetting(FeidiActionBar view) {
        view.setVisibility(View.VISIBLE);
        view.setMessageVisibility(View.GONE);
        view.setSettingVisibility(View.GONE);
        view.setFallbackVisibility(View.GONE);
        view.setScanCodeView(View.INVISIBLE);
        view.setTitleText("订单");
    }
}
